#!/usr/bin/env python
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
# 
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
# 
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

__licence__ = "GPLv2"
__copyright__ = "2008, Matt Layman"
__author__ = "Matt Layman <laymansterms.dev@gmail.com>"

import unittest
import sys
import os
from pysqlite2 import dbapi2 as sqlite

class TestMusic(unittest.TestCase):
    """This test class sets up the test database required for all test
    classes that need to access the music cache"""
    
    def setUp(self):
        self.debug = False
        # The location of album art is required for some tests
        self.art_path = os.path.expanduser('~/.entertainer/cache/album_art')
        # Create test music database
        self.TEST_DB = os.path.abspath('.' + '/data/' + self.__str__() + '.db')
        if os.path.exists(self.TEST_DB):
            os.remove(self.TEST_DB) # Start fresh to avoid table creation error
        self.connection = sqlite.connect(self.TEST_DB)
        self.cursor = self.connection.cursor()
        self.cursor.execute("""CREATE TABLE track(
                            filename TEXT,
                            title VARCHAR(255),
                            artist VARCHAR(255),
                            album VARCHAR(255),
                            tracknumber INTEGER,
                            bitrate INTEGER,
                            year INTEGER,
                            rating INTEGER DEFAULT NULL,
                            length INTEGER,
                            genre VARCHAR(128),
                            comment TEXT,
                            lyrics TEXT DEFAULT "",
                            PRIMARY KEY(filename))""")
        # Populate the test database with enough information to make it useful
        for i in range(2):
            for j in range(2):
                for k in range(2):
                    sum = i + j + k # to get unique track numbers for ablums
                    db_row = ('/filename/%s' % str(i) + str(j) + str(k),
                              'title%s' % str(i) + str(j),
                              'artist%d' % i,
                              'album%d' % j,
                              'genre%d' % i,
                              i + j + k, # length
                              sum, # tracknumber
                              i, # bitrate
                              'comment%d' % i,
                              i  # year
                              )
                    self.cursor.execute("""INSERT INTO track(filename,
                                                             title,
                                                             artist,
                                                             album,
                                                             genre,
                                                             length,
                                                             tracknumber,
                                                             bitrate,
                                                             comment,
                                                             year) 
                                        VALUES(?,?,?,?,?,?,?,?,?,?)""", db_row)

        self.cursor.execute("""CREATE TABLE playlist(
                            title TEXT,
                            PRIMARY KEY(title))""")

        self.cursor.execute("""CREATE TABLE playlist_relation(
                            title TEXT,
                            filename TEXT,
                            PRIMARY KEY(title, filename))""")
        self.connection.commit()

    def tearDown(self):
        if not self.debug:
            if os.path.exists(self.TEST_DB):
                os.remove(self.TEST_DB)
                
if __name__ == '__main__':
    suite = unittest.TestLoader().loadTestsFromTestCase(TestMusic)
    result = unittest.TextTestRunner(descriptions=2, verbosity=2).run(suite)